{% extends 'main_hub/user_base.html' %}


{% block title %}
    收藏
{% endblock %}


{% block content %}

    <div class="container">
        <hr>
        <nav class="nav nav-pills nav-fill">
            <a class="nav-link {% ifequal fav_type 'video' %}active{% endifequal %}"
               href="{% url 'user_fav' %}?page_name=user&fav_type=video">视频</a>
            <a class="nav-link {% ifequal fav_type 'user' %}active{% endifequal %}"
               href="{% url 'user_fav' %}?page_name=user&fav_type=user">用户</a>
        </nav>

        {% ifequal fav_type 'video' %}
            <h4>全部收藏</h4>
            {#    显示收藏的视频#}
            {% if all_video.object_list %}
                <div class="row">
                    {% for video in all_video.object_list %}
                        <div style="margin: 10px">
                            <div class="card" style="width:12rem;">
                                <a href="{% url 'video_detail' video.id %}?video_sub_number=1">
                                    <img class="card-img-top" src="{{ MEDIA_URL }}{{ video.image }}"
                                         style="width: 12rem">
                                </a>
                                <div class="card-body">
                                                                <h5 class="card-title info">
                                <a href="{% url 'video_detail' video.id %}?video_sub_number=1"
                                   style="text-decoration:none;color:black;font-size: 15px;font-weight: bold">
                                    {% if video.name|length > 8 %}
                                        {{ video.name|slice:'0:8' }}...
                                    {% else %}
                                        {{ video.name }}
                                    {% endif %}</a>
                            </h5>
                            <p class="card-text">
                                {% if video.info|length > 9 %}
                                    {{ video.info|slice:'0:8' }}...
                                {% else %}
                                    {{ video.info }}
                                {% endif %}</p>
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                </div>
            {% else %}
                <br>
                <h5 style="color: darkslategrey">您还没有收藏视频</h5>
            {% endif %}
{#            分页#}
            <div class="row justify-content-center">
                <div class="col-auto" style="margin-top: 50px">
                    <nav aria-label="Page navigation example">
                        <ul class="pagination" style="right:500px">
                            {% if all_video.has_previous %}
                                <li class="page-item">
                                    <a href="?{{ all_video.previous_page_number.querystring }}"
                                       class="page-link" aria-label="Previous">
                                        上一页
                                    </a>
                                </li>
                            {% endif %}
                            {% for page in all_video.pages %}
                                {% if page %}
                                    {% ifequal page all_video.number %}
                                        <li class="page-item">
                                            <a class="page-link" href="?{{ page.querystring }}">{{ page }}</a>
                                        </li>
                                    {% else %}
                                        <li>
                                            <a class="page-link page" href="?{{ page.querystring }}">{{ page }}</a>
                                        </li>
                                    {% endifequal %}
                                {% else %}
                                    <li class="none">...</li>
                                {% endif %}
                            {% endfor %}
                            {% if all_video.has_next %}
                                <li class="page-item">
                                    <a href="?{{ all_video.next_page_number.querystring }}" class="page-link"
                                       aria-label="Next">下一页</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
            </div>
        {% else %}
            {#    关注的用户#}
            <br>
            <h4>全部关注</h4>
            {% if all_star.object_list %}
                {% for star in all_star.object_list %}
                    <table class="table">
                        <tbody>
                        <tr>
                            <td>
                                <a href="{% url 'detail_user' %}?username={{ star.username }}">
                                    <img class="card-img-top" src="{{ MEDIA_URL }}{{ star.image }}"
                                         style="width: 100px; height: 100px;border-radius:50px">
                                </a>
                            </td>
                            <td style="vertical-align:middle;line-height:20px;">
                                <a href="{% url 'detail_user' %}?username={{ star.username }}"
                                   style="text-decoration:none;font-size: 20px;color: darkslategrey; left: -20px">
                                    <br>
                                    {% if star.nick_name %}
                                        {{ star.nick_name }}
                                    {% else %}
                                        {{ star.username }}
                                    {% endif %}
                                    <br>
                                    <p style="color: grey; font-size: 15px">
                                        关注数:<span id="fans_num{{ forloop.counter }}">{{ star.fans }}</span>
                                    </p>
                                </a>
                            </td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td style="vertical-align:middle;line-height:20px;">
                                <button style="width: 100px" id="jsRightBtn" type="button" class="btn btn-primary"
                                        onclick="add_user($(this),{{ star.id }},2,{{ forloop.counter }})">
                                    已关注
                                </button>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                {% endfor %}
                <div class="row justify-content-center">
                    <div class="col-auto" style="margin-top: 50px">
                        <nav aria-label="Page navigation example">
                            <ul class="pagination" style="right:500px">
                                {% if all_video.has_previous %}
                                    <li class="page-item">
                                        <a href="?{{ all_video.previous_page_number.querystring }}"
                                           class="page-link" aria-label="Previous">
                                            上一页
                                        </a>
                                    </li>
                                {% endif %}
                                {% for page in all_video.pages %}
                                    {% if page %}
                                        {% ifequal page all_video.number %}
                                            <li class="page-item">
                                                <a class="page-link" href="?{{ page.querystring }}">{{ page }}</a>
                                            </li>
                                        {% else %}
                                            <li>
                                                <a class="page-link page" href="?{{ page.querystring }}">{{ page }}</a>
                                            </li>
                                        {% endifequal %}
                                    {% else %}
                                        <li class="none">...</li>
                                    {% endif %}
                                {% endfor %}
                                {% if all_video.has_next %}
                                    <li class="page-item">
                                        <a href="?{{ all_video.next_page_number.querystring }}" class="page-link"
                                           aria-label="Next">下一页</a>
                                    </li>
                                {% endif %}
                            </ul>
                        </nav>
                    </div>
                </div>
            {% else %}
                <br>
                <h5 style="color: darkslategrey">您还没有关注用户</h5>
            {% endif %}
        {% endifequal %}
    </div>

{% endblock %}
{% block custom_jss %}
    <script>
        function add_user(current_elem, fav_id, fav_type, count) {
            $.ajax({
                cache: false,
                type: "POST",
                url: '{% url 'add_fav' %}',
                data: {'fav_id': fav_id, 'fav_type': fav_type},
                async: true,
                beforeSend: function (xhr, setting) {
                    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                },
                success: function (data) {
                    if (data.status == 'fail') {
                        if (data.msg == '用户未登录') {
                            alert(data.msg)
                        } else {
                            alert(data.msg)
                        }
                    } else if (data.status == 'success') {
                        current_elem.text(data.msg);
                        $('#fans_num'+count).text(data.num)
                    }
                }
            })
        }
    </script>
{% endblock %}